package com.magicsoft.share.presenter.exchange;

import com.magicsoft.share.base.BasePresenter;
import com.magicsoft.share.bean.exchange.GoodsDetail;
import com.magicsoft.share.constant.APIService;
import com.magicsoft.share.constant.SPKey;
import com.magicsoft.share.rx_retrofit_network.location.retrofit.RetrofitUtil;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.LoadingTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.NetWorkTransformer;
import com.magicsoft.share.rx_retrofit_network.location.rxandroid.SimpleObserver;
import com.magicsoft.share.utils.SPUtils;
import com.magicsoft.share.view.exchange.IGoodsView;

import io.reactivex.annotations.NonNull;

/**
 * Created by lyf on 2017/12/1/001.
 */

public class GoodsPresenter extends BasePresenter<IGoodsView> {

    public void goodsDetail(long id){
        RetrofitUtil.getApi(APIService.class)
                .goodsDetail(id)
                .compose(new NetWorkTransformer<GoodsDetail>())
                .compose(new LoadingTransformer<GoodsDetail>(mContext))
                .subscribe(new SimpleObserver<GoodsDetail>(mCompositeDisposable) {
                    @Override
                    public void call(@NonNull GoodsDetail goodsDetail) {
                        getView().goodsDetailSuccess(goodsDetail);
                    }

                    @Override
                    public void showErrorMsg(Throwable e, String errorMsg) {
                        super.showErrorMsg(e, errorMsg);
                        getView().goodsDetailFail();
                    }
                });
    }

    public void joinShopCart(long id,int goodsNum){
        RetrofitUtil.getApi(APIService.class)
                .joinShopCart(SPUtils.getInstance().getString(SPKey.TOKEN),SPUtils.getInstance().getLong(SPKey.ID),
                        id,goodsNum)
                .compose(new NetWorkTransformer())
                .compose(new LoadingTransformer(mContext))
                .subscribe(new SimpleObserver(mCompositeDisposable) {

                    @Override
                    public void call(@NonNull Object o) {
                        getView().joinShopCartSuccess();
                    }
                });
    }
}
